#include "MsgSyncDB.h"
#include "ConnectionPool.h"
#include "SqlQuery.h"
#include "Format.h"

// 静态对象类外初始化
MsgSyncDB *MsgSyncDB::globalInstance = new MsgSyncDB();

MsgSyncDB::MsgSyncDB()
{
    // 为每个用户维护一个同步TimeLine
    MYSQL *conn = ConnectionPool::getInstance()->getConnObj();
    SqlQuery query(conn);
    string sql = "select dd_num from user";
    if (query.exec(sql) == false)
    {
        printf("%s\n", query.getErrorText().data());
        ConnectionPool::getInstance()->recycleConnObj(conn);
        return;
    }
    // 获取结果集行数
    int rows = query.getRows();

    // 初始化(创建)每个用户的同步TimeLine
    for (int i = 0; i < rows; i++)
    {
        string uid = query.getRes()[i].front();
        deque<struct st_msg> Que;
        sync_map[atoi(uid.c_str())] = Que;
    }

    ConnectionPool::getInstance()->recycleConnObj(conn);
}

MsgSyncDB::~MsgSyncDB()
{
}

void MsgSyncDB::addMsgToSyncTimeLine(int uid, st_msg msg)
{
    m_mutex.lock();

    // 尾部添加一条消息
    sync_map[uid].push_back(msg);

    m_mutex.unlock();
}
